package com.example.ccj.b.c.ui.install;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.InputCheckWithUpAndDown;
import com.example.ccj.b.c.ui.BoMaKaiGuanControl;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.text.TextInputType;
import com.github.gzuliyujiang.colorpicker.ColorPicker;
import com.github.gzuliyujiang.colorpicker.OnColorPickedListener;
import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.google.android.material.textfield.TextInputLayout;

import java.util.ArrayList;

/**
 * 设置control  分为拨码类型，选择类型，文本类型
 * 2023年9月5日10:48:13
 */
public class SwitchCheckBoxControl extends LinearLayout {
    private BoMaKaiGuanControl checkboxControl;
    private TextView name;
    private TextView installName;
    private TextView notes;
    private TextView switchColorSelect;
    private ImageView select;
    private InstallItem installItem;
    private InputCheckWithUpAndDown installText;
    private boolean isTitle;
    private String titleName;
    private SeekBar seekBar;
    private AddOptionView addOptionView;
    private LinearLayout layout;


    private String nameP;

    public SwitchCheckBoxControl(Context context,String titleName) {
        super(context);
        isTitle = true;
        this.titleName = titleName;
        init();
    }

    public SwitchCheckBoxControl(Context context,@NonNull InstallItem installItem) {
        super(context);
        init();
        setData(installItem);
    }

    public SwitchCheckBoxControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ShowTextViewAndRightPictureControl);
        if (typedArray != null) {
            nameP = typedArray.getString(R.styleable.ShowTextViewAndRightPictureControl_text_and_img_field_name);
        }
        typedArray.recycle();
        init();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.control_switch_check_box,this);
        layout = findViewById(R.id.control_check_box_lin);
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));

        if (isTitle) {
            TextView textView = findViewById(R.id.control_check_box_title);
            layout.setVisibility(GONE);
            textView.setVisibility(VISIBLE);
            textView.setText(titleName);
        } else {
            checkboxControl = findViewById(R.id.checkbox_control);
            name = findViewById(R.id.name);
            name.setText(nameP);
            installName = findViewById(R.id.install_name);
            notes = findViewById(R.id.notes);
            select = findViewById(R.id.select);
            installText = findViewById(R.id.install_text);
            switchColorSelect = findViewById(R.id.switch_color_select);
            seekBar = findViewById(R.id.seebar);
            addOptionView = findViewById(R.id.add_option_view);
        }
    }

    public void setData(@NonNull InstallItem installItem){
        this.installItem = installItem;
        name.setText(installItem.getInstallName());
        if (!StringUtils.isEmpty(installItem.getNotes())) {
            notes.setVisibility(VISIBLE);
            notes.setText(installItem.getNotes());
        }
        initType();
    }

    private void initType(){
        switch (installItem.getType()) {
            case InstallItem.TYPE_BOMA:
                checkboxControl.setVisibility(VISIBLE);
                installName.setVisibility(GONE);
                select.setVisibility(GONE);
                installText.setVisibility(GONE);
                initBoMa();
                break;
            case InstallItem.TYPE_SELECT:
                checkboxControl.setVisibility(GONE);
                installName.setVisibility(VISIBLE);
                select.setVisibility(VISIBLE);
                installText.setVisibility(GONE);
                initSelect();
                break;
            case InstallItem.TYPE_TEXT:
                name.setVisibility(GONE);
                checkboxControl.setVisibility(GONE);
                installName.setVisibility(GONE);
                select.setVisibility(GONE);
                installText.setVisibility(VISIBLE);
                initInstallText();
                break;
            case InstallItem.TYPE_JUMP_AND_BOMA:
                checkboxControl.setVisibility(VISIBLE);
                installName.setVisibility(GONE);
                select.setVisibility(VISIBLE);
                installText.setVisibility(GONE);
                initJumpAndBoMa();
                break;
            case InstallItem.TYPE_COLOR:
                checkboxControl.setVisibility(GONE);
                installName.setVisibility(GONE);
                select.setVisibility(GONE);
                installText.setVisibility(GONE);
                switchColorSelect.setVisibility(VISIBLE);
                initColor();
                break;
            case InstallItem.TYPE_DRAG:
                checkboxControl.setVisibility(GONE);
                installName.setVisibility(VISIBLE);
                select.setVisibility(GONE);
                installText.setVisibility(GONE);
                switchColorSelect.setVisibility(GONE);
                seekBar.setVisibility(VISIBLE);
                initDrag();
                break;
            case InstallItem.TYPE_JUMP:
                checkboxControl.setVisibility(GONE);
                installName.setVisibility(GONE);
                select.setVisibility(VISIBLE);
                installText.setVisibility(GONE);
                switchColorSelect.setVisibility(GONE);
                seekBar.setVisibility(GONE);
                initJump();
                break;
            case InstallItem.TYPE_TIAN_JIA_OPTION:
                name.setVisibility(GONE);
                addOptionView.setTitleName(installItem.getInstallName());
                addOptionView.setVisibility(VISIBLE);
                checkboxControl.setVisibility(GONE);
                installName.setVisibility(GONE);
                select.setVisibility(GONE);
                installText.setVisibility(GONE);
                switchColorSelect.setVisibility(GONE);
                seekBar.setVisibility(GONE);
                initAddOption();
                break;
        }
    }

    private void initAddOption(){
        InstallAddOption installAddOption = (InstallAddOption) installItem;
        if (installAddOption.optionList == null) installAddOption.optionList = new ArrayList<>();
        addOptionView.setData(installAddOption.optionList);
    }

    private void initDrag() {
        InstallDragSelect dragSelect = (InstallDragSelect) installItem;
        seekBar.setMax(dragSelect.getMax());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            seekBar.setMin(dragSelect.getMin());
        }
        seekBar.setProgress(dragSelect.getDef());
        installName.setText(String.valueOf(dragSelect.getDef()));
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                installName.setText(String.valueOf(i));
                if (dragSelect.getSelect() != null) {
                    dragSelect.getSelect().selectNumber(i);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void initColor() {
        InstallSelectColor selectColor = (InstallSelectColor) installItem;
        switchColorSelect.setBackgroundColor(selectColor.getDefColor());
        switchColorSelect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ColorPicker colorPicker = new ColorPicker(ActivityUtils.getTopActivity());
                colorPicker.setOnColorPickListener(new OnColorPickedListener() {
                    @Override
                    public void onColorPicked(int pickedColor) {
                        switchColorSelect.setBackgroundColor(pickedColor);
                        if (selectColor.getSelect() != null) {
                            selectColor.getSelect().selectColor(pickedColor);
                        }
                    }
                });
                colorPicker.show();
            }
        });
    }

    private void initJumpAndBoMa() {
        InstallJumpAndBoMa jump = (InstallJumpAndBoMa) installItem;
        //这里一定要先设置初始值在设置点击回调
        installName.setText(jump.getBeginText());
        checkboxControl.setSelect(jump.isSelect());
        select.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                JumpOnClickListener select1 = jump.getSelect();
                if (select1 != null) {
                    jump.getSelect().jump(installName);
                }
            }
        });
        checkboxControl.setBoMaOnClickListener(jump.getBoMaOnClickListener());
    }

    private void initJump() {
        InstallJump jump = (InstallJump) installItem;
        //这里一定要先设置初始值在设置点击回调
        installName.setText(jump.getBeginText());
        select.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                JumpOnClickListener select1 = jump.getSelect();
                if (select1 != null) {
                    jump.getSelect().jump(installName);
                }
            }
        });
    }

    private void initBoMa() {
        BoMa boMa = (BoMa) installItem;
        //这里一定要先设置初始值在设置点击回调
        checkboxControl.setSelect(boMa.isSelect());
        checkboxControl.setBoMaOnClickListener(boMa.getBoMaOnClickListener());
    }

    private void initSelect(){
        InstallSelect installSelect = (InstallSelect) installItem;
        installName.setText(installSelect.getBeginText());

        layout.setOnClickListener(view -> selectOnClick(installSelect));
        select.setOnClickListener(view -> selectOnClick(installSelect));
    }


    private void selectOnClick(InstallSelect installSelect ){
        OptionPicker optionPicker = new OptionPicker(ActivityUtils.getTopActivity());
        optionPicker.setData(installSelect.getOptionList());
        optionPicker.getWheelLayout().getWheelView().setFormatter(new WheelFormatter() {
            @Override
            public String formatItem(@NonNull Object item) {
                SelectOnClickListener select1 = installSelect.getSelect();
                String format = "null";
                if (select1 != null) {
                    format = installSelect.getSelect().format(item);
                }
                return format;
            }
        });
        optionPicker.setTitle(installSelect.getInstallName());
        optionPicker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                SelectOnClickListener select1 = installSelect.getSelect();
                if (select1 != null) {
                    String change = installSelect.getSelect().change(item);
                    installName.setText(change);
                }
            }
        });
        optionPicker.show();
    }

    private void initInstallText(){
        InstallText installItem1 = (InstallText) installItem;
        installText.setText(installItem1.getValue());
        installText.setTextHint(installItem1.installName);
        installText.setCheckLength(installItem1.getMaxLength());
        if (installItem1.isMast()) installText.setMast();

        switch (installItem1.getTextType()) {
            //如果类型为数字，设置只能输入0-9最大11位
            case number:
                installText.setOnlyNumber(true);
//                TextInputType.setEditTextInPutTypeToInt(installText);
                break;
            case text:
                break;
            //如果类型为小数，设置只能输入0-9和.保留2位小数最大18位
            case xiao:
                installText.setOnlyDouble(true);
//                TextInputType.setEditTextInPutTypeToDouble(installText);
                break;
            //如果类型为电话，设置只能输入0-9最大11位
            case phone:
                TextInputType.setEditTextInPutTypeToPhone(installText.getTextInputEditText());
                break;
        }

        installText.setTextModifyListener(new InputCheckWithUpAndDown.TextModify() {
            @Override
            public void onTextModify(String sqlFieldName, String text) {
                TextChangeListener textChangeListener = installItem1.getTextChangeListener();
                if (textChangeListener != null) {
                    installItem1.getTextChangeListener().onClick(text);
                }
            }
        });

    }

    public BoMaKaiGuanControl getCheckboxControl() {
        return checkboxControl;
    }

    public void setCheckboxControl(BoMaKaiGuanControl checkboxControl) {
        this.checkboxControl = checkboxControl;
    }

    public String getTitleName() {
        return titleName;
    }

    public void setTitleName(String titleName) {
        this.titleName = titleName;
    }

    public TextView getName() {
        return name;
    }

    public TextView getInstallName() {
        return installName;
    }

    public TextView getNotes() {
        return notes;
    }

    public ImageView getSelect() {
        return select;
    }

    public InstallItem getInstallItem() {
        return installItem;
    }

    public InputCheckWithUpAndDown getInstallText() {
        return installText;
    }

    public boolean isTitle() {
        return isTitle;
    }

}



